<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/7/23
 * Time: 0:03
 */
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class AdminController extends Controller
{
    public function __construct()
    {
        if(empty(session('Z_AdminiUser')))
        {
            header('Location:'.__BASE__.'/admini/log');
            die();
        }else{
            //全局变量
            $user = json_decode(session('Z_AdminiUser'),true);
            if(!empty($user['group_servant_id'])){
                $groupStr = $user['group_master_id'].','.$user['group_servant_id'];
            }else{
                $groupStr = $user['group_master_id'];
            }
            $GLOBALS['leftMenu'] = self::createLeftMenu($groupStr,$user['id']);
            $this->ViewAccess($groupStr);
        }

    }

    /**
     * 获取左侧菜单栏目权限节点...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <Y-m-d H:i:s>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:...>
     * @param $groupStr
     * @param $uid
     * @return array
     */
    private static function createLeftMenu($groupStr,$uid){

            //获取当前所有权限节点
            $allNode = DB::select('
                                    select id,name,node_name,pid,sort,concat(path,",",id) as bpath
                                    from wj_admini_node
                                    where is_show = ?
                                    and is_del = ?
                                    order by bpath
                                ',[1,1]);
            //超级管理员直接给予所有权限
            $isAdmini = explode(',',$groupStr);
            if(in_array(1,$isAdmini)){
                return $allNode;
            }
            $arr = self::UserNode($allNode,$groupStr);
            return $arr;
    }

    /**
     * 从所有权限中剔除用户不存在的权限
     * @access <private>
     * @name <UserNode>
     * @param <obj> <$all_node> <所有权限对象>
     * @param <str> <$grou_str> <用户拥有权限str标识>
     * @param <int> <$is_node_name> <是否返回标识数组>
     * @return <array> <剔除后的数据>
     */
    private static function UserNode($allNode,$groupStr,$isNodeName = 0)
    {
        if(empty($allNode)) return array();
        //当前所拥有权限
        $allNodeArr = DB::select('
                                select node_id
                                from wj_admini_group_node
                                where group_id in ('.$groupStr.')
                            ');
        if(empty($allNodeArr)) return array();
        $objToArr = array();
        foreach($allNodeArr as $k => $v){
            $objToArr[] = $v->node_id;
        }
        $objToArr = array_unique($objToArr);
        $arr = array();
        //去除没有的权限节点数据并返回数据
        foreach($allNode as $value){
            if(in_array($value->id,$objToArr))
            {
                if($isNodeName == 1)
                {
                    $arr[] = $value->node_name;
                }else {
                    $arr[] = $value;
                }
            }
        }
        return $arr;
    }

    /**
     * 验证用户访问路径是否有权限
     * @access <private>
     * @name <ViewAccess>
     * @param <string> <$groupStr> <用户权限字符>
     * @return <pass or die('无权访问')>
     */
    private function ViewAccess($groupStr)
    {
        header("Content-type:text/html;charset=utf-8");
        //获取当前所有权限节点
        $allNode = DB::select('
                                    select id,name,node_name,pid,concat(path,",",id) as bpath
                                    from wj_admini_node
                                    where is_del = ?
                                    order by bpath
                                ',[1]);
        //含有超级管理员组给予所有的权限通过
        $isAdmini = explode(',',$groupStr);
        if(in_array(1,$isAdmini))
        {
            foreach ($allNode as $value)
            {
                $arr[] = $value->node_name;
            }
        }else{
            $arr = $this->UserNode($allNode,$groupStr,1);
        }
        if(empty($arr)) die('无权访问');
        $route = \Route::current();
        $url = '/'.$route->getUri();
        //获取真实访问url信息集合
        $adminiPath = \Route::getCurrentRequest();
        //涉及超级管理员越权操作规避
        $adminiArrNode = array(

        );
        //越权操作验证
        if(in_array($adminiPath->getPathInfo(),$adminiArrNode)){
            die('无权访问');
        }
        //dd($url);
        //默认权限验证
        if(!in_array($url,$arr)){
            die('无权访问');
        }
    }
}